package com.juyu.miliao.scheduler.message;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.juyu.miliao.common.constant.UserWalletBeansSumMqKey;
import com.juyu.miliao.common.domain.BillDetailBeans;
import com.juyu.miliao.common.domain.dto.BillDetailBeansDTO;
import com.juyu.miliao.scheduler.dao.mysql.BillDetailBeansDao;
import com.juyu.miliao.scheduler.dao.mysql.UserWalletDao;
import com.juyu.miliao.scheduler.domin.UserWallet;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * @author xuanqicheng
 */
@Component
@Slf4j
public class UserWalletBeansMq {

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private UserWalletDao userWalletDao;

    @Autowired
    private BillDetailBeansDao billDetailBeansDao;


    /**
     * 新注册用户送寻豆，每天送寻豆
     * @param message
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(UserWalletBeansSumMqKey.USER_WALLET_BEANS_QUEUE_KEY),
            exchange = @Exchange(UserWalletBeansSumMqKey.USER_BEANS_EXCHANGE_KEY),
            key =UserWalletBeansSumMqKey.USER_WALLET_BEANS_ROUNTING_KEY))
    public void getUserWalletBeansMqMessage(String message){
        if(!StringUtils.isEmpty(message)){
            try {
                log.info("处理寻豆:[{}]",message);
                BillDetailBeansDTO billDetailBeansDTO = objectMapper.readValue(message,BillDetailBeansDTO.class);
                UserWallet userWallet = new UserWallet();
                userWallet.setBeans(billDetailBeansDTO.getBeans());
                userWallet.setUserId(billDetailBeansDTO.getUserId());
                int flag = userWalletDao.plusUserWalletBeans(userWallet);
                if(flag>0){
                    BillDetailBeans billDetailBeans = new BillDetailBeans();
                    BeanUtils.copyProperties(billDetailBeansDTO,billDetailBeans);
                    billDetailBeansDao.insert(billDetailBeans);
                }
            }catch (Exception ex){
                log.error("处理寻豆据错误：" + message + "|error=" + ex.getMessage());
            }
        }
    }




    public void sendUserWalletBeansSumMq(BillDetailBeansDTO beansDTO){
        try {
            if (beansDTO != null) {
                amqpTemplate.convertAndSend(UserWalletBeansSumMqKey.USER_BEANS_EXCHANGE_KEY,
                        UserWalletBeansSumMqKey.USER_WALLET_BEANS_ROUNTING_KEY,
                        objectMapper.writeValueAsString(beansDTO));
            }
        }catch (Exception ex){
            log.info("vip添加寻豆信息放入队列失败：{}", beansDTO);
        }
    }

}
